package com.qbw.app.module.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lib.util.ScreenUtil;
import com.qbw.app.R;

/**
 * @author QBW
 * @date 2019/9/16
 */
public class VerifyCodeView extends LinearLayout {

    private int mCount;
    private int mItemWidth;
    private int mItemHeight;
    private int mHorizontalMargin;
    private int mTextColor;
    private int mTextSize;

    public VerifyCodeView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public VerifyCodeView(@NonNull Context context,
                          @Nullable AttributeSet attrs,
                          int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attributeSet) {
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet,
                                                               R.styleable.VerifyCodeView);
        mCount = typedArray.getInteger(R.styleable.VerifyCodeView_vc_count, 4);
        int size = ScreenUtil.dp2px(context, 50);
        mItemWidth = (int) typedArray.getDimension(R.styleable.VerifyCodeView_vc_item_width, size);
        mItemHeight = (int) typedArray.getDimension(R.styleable.VerifyCodeView_vc_item_height,
                                                    size);
        mHorizontalMargin = (int) typedArray.getDimension(R.styleable.VerifyCodeView_vc_item_horizontal_margin,
                                                          ScreenUtil.dp2px(context, 17));
        mTextColor = typedArray.getColor(R.styleable.VerifyCodeView_vc_item_text_color,
                                         context.getResources().getColor(android.R.color.white));
        mTextSize = (int) typedArray.getDimension(R.styleable.VerifyCodeView_vc_item_text_size,
                                                  ScreenUtil.sp2px(context, 17));
        onAddViews();
        typedArray.recycle();
    }

    private void onAddViews() {
        for (int i = 0; i < mCount; i++) {
            addView(onCreateView(i));
        }
    }

    private View onCreateView(int index) {
        TextView textView = new TextView(getContext());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mItemWidth, mItemHeight);
        params.gravity = Gravity.CENTER;
        if (index > 0) {
            params.leftMargin = mHorizontalMargin;
        }
        textView.setLayoutParams(params);
        textView.setGravity(Gravity.CENTER);
        textView.setBackgroundResource(R.drawable.verifycode_selector);
        textView.setTextColor(mTextColor);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
        return textView;
    }

    public boolean isInputAllNumber() {
        int cc = getChildCount();
        if (cc > 0) {
            TextView tv = (TextView) getChildAt(cc - 1);
            if (!TextUtils.isEmpty(tv.getText())) {
                return true;
            }
        }
        return false;
    }

    public void onInputNumber(int number) {
        if (isInputAllNumber()) {
            return;
        }
        int cc = getChildCount();
        TextView tv;
        for (int i = 0; i < cc; i++) {
            tv = (TextView) getChildAt(i);
            if (TextUtils.isEmpty(tv.getText())) {
                tv.setText(String.valueOf(number));
                tv.setSelected(true);
                break;
            } else {
                tv.setSelected(false);
            }
        }
    }

    public void onDel() {
        int cc = getChildCount();
        TextView tv;
        for (int i = cc - 1; i >= 0; i--) {
            tv = (TextView) getChildAt(i);
            if (!TextUtils.isEmpty(tv.getText())) {
                tv.setText("");
                tv.setSelected(false);
                if (i - 1 >= 0) {
                    getChildAt(i - 1).setSelected(true);
                }
                break;
            }
        }
    }

    public String getInputText() {
        StringBuilder sb = new StringBuilder();
        int cc = getChildCount();
        TextView tv;
        for (int i = 0; i < cc; i++) {
            tv = (TextView) getChildAt(i);
            sb.append(tv.getText());
        }
        return sb.toString();
    }
}
